package service

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"path"
	"videosS_api/basic/config"
	__ "videosS_api/basic/proto"
	"videosS_api/handler/model"
	"videosS_api/pkg"
)

func SendSms(c *gin.Context) {
	var form model.SendSms
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	_, err := config.VideoCon.SendSms(c, &__.SendSmsReq{
		Username: form.UserName,
	})
	
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "发送失败",
		})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 400,
		"msg":  "短信发送成功",
	})
	return
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		return
	}
	ext := path.Ext(file.Filename)
	if ext != ".jpg" && ext != "png" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "上传文件格式不对",
		})
		return
	}

	uploads, err := pkg.Upload(file.Filename, "upload")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "上传文件上传失败",
		})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 200,
		"msg":  "上传文件上传成功",
		"data": uploads,
	})
	return
}
